﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using SPAzureBlobField.Services;

namespace SPAzureBlobField.Classes
{
    public class AzureBrowserConnections
    {
        // Static dictionaries to hold SL clients access.
        private static Dictionary<int, DateTime> _lastTimeUsersAlive = new Dictionary<int, DateTime>();
        private static Dictionary<int, List<Blob>> _currentUserConnections = new Dictionary<int, List<Blob>>();

        public static void AddSession(SPUser user)
        {
            _currentUserConnections.Add(user.ID, new List<Blob>());
            _lastTimeUsersAlive.Add(user.ID, DateTime.Now);
        }

        public static void RemoveSession(SPUser user)
        {
            _currentUserConnections.Remove(user.ID);
        }

        public static bool IsSessionOpenedForUser(SPUser user)
        {
            if (_currentUserConnections.ContainsKey(user.ID))
            {
                if (DateTime.Now.Subtract(_lastTimeUsersAlive[user.ID]).Seconds > 6)
                {
                    _currentUserConnections.Remove(user.ID);
                    _lastTimeUsersAlive.Remove(user.ID);
                    return false;
                }
                return true;
            }
            return false;
        }

        public static void AddBlob(SPUser user, Blob blob)
        {
            if (!IsSessionOpenedForUser(user))
                AddSession(user);

            var blobsIn = _currentUserConnections[user.ID].Where(
                 blobIn => blobIn.SiteId == blob.SiteId && blobIn.WebId == blob.WebId
                     && blobIn.ListId == blob.ListId && blobIn.ItemId == blob.ItemId);
            if (blobsIn.Count() == 0)
            {
                _currentUserConnections[user.ID].Add(blob);
            }
        }

        public static List<Blob> GetBLobs(SPUser user)
        {
            IEnumerable<Blob> IEbBobsToSend = _currentUserConnections[user.ID].Where(blob => !blob.SentToSLClient);
            List<Blob> ListBlobsToSend = new List<Blob>(IEbBobsToSend);
            ListBlobsToSend.ForEach(blob => blob.SentToSLClient = true);
            return ListBlobsToSend;
        }

        public static void UpdateUserAlive(SPUser user, DateTime dateTime)
        {
            _lastTimeUsersAlive[user.ID] = DateTime.Now;
        }

        public static bool IsBLobCUrrentlyUpdated(Guid siteId, Guid webId, Guid listId, int itemId)
        {
            foreach (List<Blob> blobs in _currentUserConnections.Values)
            {
                var blobsIn = blobs.Where(
                 blobIn => blobIn.SiteId == siteId && blobIn.WebId == webId
                     && blobIn.ListId == listId && blobIn.ItemId == itemId);
                if (blobsIn.Count() > 0)
                {
                    return true;
                }
            }
            return false;
        }

        public static void ValidateBLob(SPUser user, string guid, string newName)
        {
            var blob = _currentUserConnections[user.ID].Where(blobIn => blobIn.Guid == guid).Single();

            SPSite site = new SPSite(blob.SiteId, user.UserToken);
            SPWeb web = site.OpenWeb(blob.WebId);
            web.AllowUnsafeUpdates = true;
            SPList list = web.Lists[blob.ListId];
            SPListItem listItem = list.GetItemById(blob.ItemId);
            SPAzureBlobField field = listItem.Fields[blob.FieldId] as SPAzureBlobField;

            listItem[field.Title] = newName;
            listItem.Update();

            _currentUserConnections[user.ID].Remove(blob);

            web.Dispose();
            site.Dispose();
        }
    }
}
